package com.game.util;

import java.util.ArrayList;
import java.util.List;

import com.game.pojo.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 用户信息工具类
 * @author Administrator
 *
 */
public class UserUtils {
	/**
	 * 服务器返回的用户信息json串，解析为用户对象
	 * @param json
	 * @return
	 */
	public static User jsonToUser(String json){
		System.out.println(json);
		User user = new User();
		JSONObject jsonObject = JSONObject.fromObject(json);
		user.setCity(jsonObject.getString("city"));
		user.setCountry(jsonObject.getString("country"));
		user.setGroupid(jsonObject.getInt("groupid"));
		user.setHeadimgurl(jsonObject.getString("headimgurl"));
		user.setLanguage(jsonObject.getString("language"));
		//user.setNickName(jsonObject.getString("nickname"));
		user.setOpenId(jsonObject.getString("openid"));
		user.setProvince(jsonObject.getString("province"));
		user.setRemark(jsonObject.getString("remark"));
		user.setSex(Integer.parseInt(jsonObject.getString("sex")));
		user.setSubscribe_scene(jsonObject.getString("subscribe_scene"));
		user.setSubscribe_time(Long.parseLong(jsonObject.getString("subscribe_time")));
		List<String> list = new ArrayList<>();
		JSONArray tagid_list = jsonObject.getJSONArray("tagid_list");
		for (Object o : tagid_list) {
			list.add((String)o);
		}
		user.setTagid_list(list);
		
		return user;
	}
	
	/**
	 * 根据微信服务器返回的json解析用户openId
	 * @param json
	 * @return
	 */
	public static List<String> jsonToUserList(String json){
		JSONObject jsonObject = JSONObject.fromObject(json);
		JSONObject data = (JSONObject) jsonObject.get("data");
		JSONArray openIdList = data.getJSONArray("openid");
		return openIdList;
	}
}
